from wxpy import *
import time
import re
#bot = Bot()
import requests
from bs4 import BeautifulSoup 
import re
import pypinyin
from yueche import msghelp
from newfileaddman import yuerenhelp
from tohand import tohandhelp
flag = False
timenow = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
def hp(word):
    s = ''
    for i in pypinyin.pinyin(word,style=pypinyin.NORMAL):
        s += ''.join(i) 
    return s

def get_weather(city):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36','Host': 'www.tianqi.com'} 
    link = 'https://www.tianqi.com/'+city+'/'
    r = requests.get(link, headers=headers) 
    r.encoding = r.apparent_encoding
    soup = BeautifulSoup(r.text, "html.parser")
    renow = r'<p class="now"><b>(.[^&]*?)</b><i>℃</i></p>'
    nowweather = re.findall(renow,r.text)
    retodayweather = r'<span><b>(.[^&]*?)</b>(.[^&]*?)</span>'
    temp = soup.find_all('dd',class_='weather')
    todayweather = re.findall(retodayweather,str(temp))
    retodashidu = r'<dd class="shidu"><b>(.[^&]*?)</b><b>(.[^&]*?)</b><b>(.[^&]*?)</b></dd>'
    shidu = soup.find_all('dd',class_='shidu')
    todayshidu = re.findall(retodashidu,str(shidu))
    rekongqi = r'<dd class="kongqi"><h5 style="(.[^&]*?)">(.[^&]*?)</h5><h6>(.[^&]*?)</h6><span>(.[^&]*?)<br/>(.[^&]*?)</span></dd>'
    kongqi = soup.find_all('dd',class_='kongqi')
    todaykongqi = re.findall(rekongqi, str(kongqi))
    #明后天天气
    furweather = soup.find_all('ul',class_="txt txt2")
    refurweather = r'<li>(.[^&]*?)</li>'
    newfurweather = re.findall(refurweather,str(furweather))
    furtemp = soup.find_all('div',class_="zxt_shuju")
    refurtemp = r'<li><span>(.[^&]*?)</span><b>(.[^&]*?)</b></li>'
    newfurtemp = re.findall(refurtemp,str(furtemp))
    #print(newfurtemp[1][1],'~',newfurtemp[1][0])
    #print(newfurtemp[2][1], '~', newfurtemp[2][0])

    weather = ('当前温度:%s℃\n今天天气:%s\t%s\n%s\n%s\n%s\n%s\n%s\n%s    %s\n*****************\n明天:%s  %s~%s\n后天:%s  %s~%s'%(nowweather[0],todayweather[0][0],todayweather[0][1],todayshidu[0][0],todayshidu[0][1],todayshidu[0][2],todaykongqi[0][1],todaykongqi[0][2],todaykongqi[0][3],todaykongqi[0][4],newfurweather[1],newfurtemp[1][1],newfurtemp[1][0],newfurweather[2],newfurtemp[2][1],newfurtemp[2][0]))
    return weather

bot = Bot(cache_path = True)
print("登录成功")
#Jski机器人
Jski_Robot = bot.groups().search("JSKI极四季.中国西部雪友群 ")[0]
Jski_yueche = bot.groups().search("成都融创自动拼车拼房群")[0]
Jski_yueche2 = bot.groups().search("成都融创自动拼车拼房2群")[0]
#暗黑欢乐群机器人
Diablo_Robot = bot.groups().search("暗黑欢乐群-21赛季")[0]
##娱乐机器人
Test_Robot = bot.groups().search("五五开黑小队")[0]
Test_Robot1 = bot.groups().search("boomshakalaka")[0]
Fm_Robot = bot.groups().search("我们在一起Together")
# 自动添加好友机器人
allText ="回复'1'进入暗黑欢乐群-21赛季\n回复'2'进入成都融创拼车拼房群"
@bot.register(msg_types=FRIENDS)
def auto_audit_msg(msg):
    new_friend = bot.accept_friend(msg.card)
    new_friend.send('我是群管理机器人')
    new_friend.send(allText)
@bot.register(Friend,TEXT)
def auto_accept_friends(fdmsg):
    print("获取好友消息",fdmsg)
    fdname = str(fdmsg.chat)
    fdname = fdname.replace('Friend','好友',1)
    #fdmsg.forward(bot.file_helper,prefix='%s消息'%(fdname))
    # 监听并发送给自己#
    #print(fdmsg.text)
    if "进群" in fdmsg.text or '拉我' in fdmsg.text or '赛季' in fdmsg.text or '暗黑' in fdmsg.text or '加群' in fdmsg.text:
        fdmsg.chat.send('''回复'1'进入暗黑欢乐群-21赛季
回复'2'进入成都融创自动拼车拼房群''')
    elif fdmsg.text == "1":
        Diablo_Robot.add_members(fdmsg.chat,use_invitation=True)
    elif fdmsg.text == "2":
        Jski_yueche.add_members(fdmsg.chat,use_invitation=True)
    elif fdmsg.text == "3":
        #Jski_yueche2.add_members(fdmsg.chat,use_invitation=True)
        fdmsg.chat.send('抱歉暂时还没开通，请加1群')
    elif '天气' in fdmsg.text:
        if '新都' in fdmsg.text or '尖峰'in fdmsg.text or '尖锋' in fdmsg.text:
            fdmsg.chat.send('%s\n%s'%('新都',get_weather('xindu')))
        elif '阿勒泰' in fdmsg.text:
            fdmsg.chat.send('%s\n%s'%('阿勒泰',get_weather('aletai')))
        else:
            word = fdmsg.text
            word = word.replace('天气','')
            city = hp(word)
            fdmsg.chat.send('%s\n%s'%(word,get_weather(city)))

#新人入群通知的匹配正则
rp_new_member_name = (    
        re.compile(r'^"(.+)"通过'),  
        re.compile(r'邀请"(.+)"加入'),
)
#获取新成员名称
def get_new_mumber_name(msg):    
    from itchat.utils import msg_formatter
    msg_formatter(msg.raw,'Text')    
    for rp in rp_new_member_name:   
        match = rp.search(msg.text) 
        if match:            
            return match.group(1)
#Jski新人进群欢迎语
jski_welcome_text = '''欢迎新朋友@{0} !
JSKI.极四季是全日本官方注册的最大华人滑雪俱乐部，本群是JSKI中国西部滑雪俱乐部雪友群。本群宗旨为开心滑雪，安全滑雪，欢迎群友进行文明友好的技术讨论和滑雪活动交流。
***群公告***
1.成都尖峰旱雪场67元/次/人的雪票福利。具体请联系群主预约，限本群雪友使用。
2.单板教学联系人:天一 和 陈6岁
3.免费视频滑行分析服务联系人:天一 和 陈6岁
{1}'''
#暗黑群新人进群欢迎语
diablo_welcome_text = '''机器人消息：
欢迎奈非天@{0} 进群！
进群修改昵称：ID+SW码
本群禁止讨论修改和SL
{1}'''
# Jski群设置新人进群自动@新人文本消息
@bot.register(Jski_Robot, NOTE)
def print_group_msg(msg):  
    if "加入了群聊" in msg.text:     
        #获取新成员名称        
        name = get_new_mumber_name(msg) 
        #在群里回复文本        
        msg.reply(jski_welcome_text.format(name,""))
jski_gonggao = '''***JSKI西南公告更新***
1. 单板教学联系人 六岁 天一
2. 免费滑行视频分析服务，联系人 六岁 天一
'''
@bot.register(Jski_yueche2, TEXT)
def jski_yueche_msg(gpmsg):
    if 'j' == gpmsg.text[0:1]:
        strmsg = str(gpmsg.member)
        rename =r'<Member: (.[^&]*?)>'
        name = (re.findall(rename,strmsg))[0]
        name = str(name)
        if 'j2' == gpmsg.text[0:2]:
            gpmsg.reply(tohandhelp(gpmsg.text,name,2))
        else:
            gpmsg.reply(msghelp(gpmsg.text,name,2))

@bot.register(Jski_yueche, TEXT)
def jski_yueche_msg(gpmsg):
    if 'j' == gpmsg.text[0:1]:
        strmsg = str(gpmsg.member)
        rename =r'<Member: (.[^&]*?)>'
        name = (re.findall(rename,strmsg))[0]
        name = str(name)
        if 'j2' == gpmsg.text[0:2]: 
            gpmsg.reply(tohandhelp(gpmsg.text,name,1))
        else:
            gpmsg.reply(msghelp(gpmsg.text,name,1))
    if "天气" in gpmsg.text:
        if '新都' in gpmsg.text or '尖峰' in gpmsg.text or '尖锋' in gpmsg.text:
            gpmsg.reply('%s\n%s'%('新都',get_weather('xindu')))
        elif '阿勒泰' in gpmsg.text:
            gpmsg.reply('%s\n%s'%('阿勒泰',get_weather('aletai'))) 
        else:   
            #在群里回复文本
            word = gpmsg.text
            word = word.replace('天气','')      
            city = hp(word) 
            gpmsg.reply('%s\n%s'%(word,get_weather(city)))
@bot.register(Jski_Robot, TEXT)  
def print_group_msg(gpmsg):
    if 'j' == gpmsg.text[0:1]:
        strmsg = str(gpmsg.member)
        rename =r'<Member: (.[^&]*?)>'
        name = (re.findall(rename,strmsg))[0]
        #gpmsg.reply('%s\n\n%s'%(msghelp(gpmsg.text,name),skihelp(gpmsg.text,name)))
        gpmsg.reply(yuerenhelp(gpmsg.text,name))
    if "天气" in gpmsg.text:
        if '新都' in gpmsg.text or '尖峰' in gpmsg.text or '尖锋' in gpmsg.text:
            gpmsg.reply('%s\n%s'%('新都',get_weather('xindu')))
        elif '阿勒泰' in gpmsg.text:
            gpmsg.reply('%s\n%s'%('阿勒泰',get_weather('aletai')))
        else:
            #在群里回复文本
            word = gpmsg.text
            word = word.replace('天气','')
            city = hp(word)
            gpmsg.reply('%s\n%s'%(word,get_weather(city)))

@bot.register(Fm_Robot, TEXT)
def print_group_msg(gpmsg):
    if "天气" in gpmsg.text:
        if '新都' in gpmsg.text or '尖峰' in gpmsg.text or '尖锋' in gpmsg.text:
            gpmsg.reply('%s\n%s'%('新都',get_weather('xindu')))
        elif '阿勒泰' in gpmsg.text:
            gpmsg.reply('%s\n%s'%('阿勒泰',get_weather('aletai')))
        else:
            print(gpmsg)
        #在群里回复文本
            word = gpmsg.text
            word = word.replace('天气','')
            city = hp(word)
            gpmsg.reply('%s\n%s'%(word,get_weather(city)))

# 暗黑群设置新人进群自动@新人文本消息
@bot.register(Diablo_Robot, NOTE)
def print_group_msg(msg):    
    print(msg)    
    if "加入了群聊" in msg.text:    
        #获取新成员名称        
        name = get_new_mumber_name(msg) 
        #在群里回复文本        
        msg.reply(diablo_welcome_text.format(name,""))

@bot.register(Diablo_Robot, TEXT)
def print_group_msg(gpmsg):
    if "天气" in gpmsg.text:
        #在群里回复文本
        word = gpmsg.text
        word = word.replace('天气','')
        city = hp(word)
        gpmsg.reply('%s\n%s'%(word,get_weather(city)))
# 图灵机器人 每天100条
tuling = Tuling(api_key='9ac422076bb84ddfae4498a1ab32992d') 
# 使用图灵机器人自动与指定好友聊天
@bot.register(Test_Robot ,TEXT)
def reply_my_friend(msg):
    strmsg = str(msg.member)
    rename =r'<Member: (.[^&]*?)>'
    name = (re.findall(rename,strmsg))[0]    
    #print(name)
    #print(msg.text)
    #if '.j' == msg.text[0:2]:
        #msg.reply(msghelp(msg.text,name))
    if '@刘力铭' in msg.text:        
        tuling.do_reply(msg)

@bot.register(Test_Robot1)
def reply_my_friend(msg):
    if '@刘力铭' in msg.text:
        tuling.do_reply(msg)

print("线程启动完成")
#获取微信群
#wx_groups = bot.groups()
#print("微信群总数",len(wx_groups),wx_groups)
print("Jski群友数量",len(Jski_Robot))
print("暗黑20赛季群友数量",len(Diablo_Robot))
print(timenow)
#堵塞线程
bot.join()
